/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DigestControllers;

import DigestModels.Database;
import DigestViews.AddFood;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;




/**
 *
 * @author Prophet,Sheamus,Tony
 */
public class AddFoodController {
    
    Database d;
    AddFood a;
    ActionListener actionListener;

    /*
     * A method that takes a database and an AddFood class to initialise
     * @param Database d
     * @param AddFood a
     */
    public AddFoodController(Database d, AddFood a) {
        this.d = d;
        this.a = a;
        a.setVisible(true);       
    }

    /*
     * A method that controls operations
     */
    public void control() {
        actionListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == a.getSaveBtn()){
                    save();
                } 
                else if(e.getSource() == a.getExitBtn()){
                    a.dispose();
                }              
            }
        };        
       
        a.getSaveBtn().addActionListener(actionListener);
        a.getExitBtn().addActionListener(actionListener);
        
    }
     
     
     private void save(){
         String name = a.getMealName().getText();
         double price = Double.parseDouble(a.getMealPrice().getText());
         int qty = Integer.parseInt(a.getFoodQty().getText());
         String type = (String)a.getComboBox().getSelectedItem();
         boolean b = d.addFood(name,price,qty,type);
         if(b){
             JOptionPane.showMessageDialog(a,"Added Succesfully");
         }else{
             JOptionPane.showMessageDialog(a,"Failed to Add");
         }
         
     }
    
    
}

